/**
 * @(#) AttachFileSchoolExpProxy.as
 *
 * @date		2011. 03. 04.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic 
 */
package com.skt.common_hr.healthchkreq.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.healthchkreq.model.vo.healthchkreqFileUpLoadVO;
	import com.skt.common_hr.healthchkreq.view.components.fu.hbFileUpload;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;

	public class AttachFileHealthchkreqProxy extends CommonProxy
	{
		public static const NAME:String = "AttachFileHealthchkreqProxy";
		
		public static const SELECTED_FILE:String = "selectedFile";
		public static const PROGRESS_UPLOAD:String = "progressUpload";
		public static const COMPLETE_UPLOAD:String = "completeUpload";
		public static const FAILED_UPLOAD:String = "failedUpload";
		
		private var fileRef:FileReference;

		//public var fileVo:healthchkreqFileUpLoadVO;
		//public var prtFileVo:healthchkreqFileUpLoadVO;
		
		public var bytesLoaded:uint = 0;
		public var bytesTotal:uint = 0;
		
		public function AttachFileHealthchkreqProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public var _parentObj:hbFileUpload;
		public function browse(parentObj:hbFileUpload):void
		{
			_parentObj = parentObj;
			//fileVo = new healthchkreqFileUpLoadVO();
			
 			fileRef = new FileReference();
            fileRef.addEventListener(Event.SELECT, selectedFile);
            fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
            // 저장된 Director를 얻기 위해 Event.COMPLETE 대신 DataEvent를 사용한다.
            //fileRef.addEventListener(Event.COMPLETE, onCompleteUpload);
            fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onCompleteData);
            fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            fileRef.browse();
		}
		
		public function deleteFile():void
		{
			
		}
		
		private function selectedFile(e:Event):void
        {
        	_parentObj.fileVo.I_FILE_NAME = (fileRef.name==null) ? "":fileRef.name; //replaceAll(fileRef.name," ","_");
			_parentObj.fileVo.I_FILE_EXT  = (fileRef.type==null) ? "":fileRef.type.substr(1);
			_parentObj.fileVo.I_FILE_SIZE = fileRef.size;
			
//			facade.sendNotification(SELECTED_FILE, this);
			onSelectedFile();
        }
        
        private function onSelectedFile():void
		{
			_parentObj.fileName = _parentObj.fileVo.I_FILE_NAME;
		}
		
		public function uploadFile(parentfileVo:healthchkreqFileUpLoadVO):void
		{	
			if(fileRef!=null) {
				//prtFileVo = parentfileVo;
				var urlRequest:URLRequest = new URLRequest("../FileUpload");
				fileRef.upload(urlRequest);				
			}

		}
		
		private function onProgress(event:ProgressEvent):void
		{
			bytesLoaded = event.bytesLoaded;
			bytesTotal  = event.bytesTotal;
//			facade.sendNotification(PROGRESS_UPLOAD, this);
		}
		/* 
		사용안하는듯
		private function onCompleteUpload(event:Event):void
		{
			bytesLoaded = 0;
			bytesTotal  = 0;
			facade.sendNotification(COMPLETE_UPLOAD, this);
		}
		 */
		 
		private function onCompleteData(event:DataEvent):void
		{
			_parentObj.fileVo.I_FILE_URL = event.data; 
		
//			prtFileVo.I_DV 			= fileVo.I_DV;
//			prtFileVo.I_FILE_EXT 	= fileVo.I_FILE_EXT;
//			prtFileVo.I_FILE_NAME 	= fileVo.I_FILE_NAME;
//			prtFileVo.I_FILE_SIZE 	= fileVo.I_FILE_SIZE;
//			prtFileVo.I_FILE_URL 	= fileVo.I_FILE_URL;
			
			_parentObj.childUploadCntChk();
			
		}
		
		private function onIOError(event:IOErrorEvent):void 
		{
			trace("IOError: " + event);
			facade.sendNotification(FAILED_UPLOAD, this);
		}
		
 		private function replaceAll(str:String, strPattern:String, strObject:String ):String
 		{
	    	if (str == "" || str == null) return "";
				while(str.indexOf(strPattern) >= 0)
				{
					str = str.replace(strPattern,strObject);  
				}
				return str;
		}
	}
}